#include "Stream.h"

namespace sylar
{
	Stream::Stream()
	{

	}


	int Stream::readFixSize(void* buffer, size_t length)
	{
		size_t offset = 0;
		size_t left = length;
		while (left > 0)
		{
			size_t len = read(((char*)buffer+offset), left);
			if (len <= 0)
			{
				return len;
			}
			offset += len;
			left -= len;

		}
		return length;
	}

	int Stream::readFixSize(ByteArray::ptr buffer, size_t length)
	{

		size_t left = length;
		while (left > 0)
		{
			int len = read(buffer, left);
			if (len <= 0)
			{
				return len;
			}
			left -= len;

		}
		return (int)length;
	}

	int Stream::writeFixSize(const void* buffer, size_t length)
	{
		size_t offset = 0;
		size_t left = length;
		while (left > 0)
		{
			int len = write((char*)buffer + offset, left);
			if (len <= 0)
			{
				return len;
			}
			offset += len;
			left -= len;

		}
		return (int)length;
	}

	int Stream::writeFixSize(ByteArray::ptr buffer, size_t length)
	{
	
		size_t left = length;
		while (left > 0)
		{
			size_t len = write(buffer, left);
			if (len <= 0)
			{
				return len;
			}
			
			left -= len;

		}
		return length;
	}

}
